package com.jnc.common.util.convert;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: jjn
 * @Date: 2018/9/17
 * @Desc: 字符串工具类
 */
public class StringUtil {

    private static final String REGEX_TYPE = "_(\\w)";
    private static final String UNDER_LINE = "_";

    /**
     * 下划线字符串转驼峰命名(大写先转小写，首字母小写)
     * @param str
     * @return
     */
    public static String toLowerCamelCase(String str){
        if(null == str){
            return null;
        }
        str = str.toLowerCase();
        final StringBuffer sb = new StringBuffer();
        Pattern p = Pattern.compile(REGEX_TYPE);
        Matcher m = p.matcher(str);
        while(m.find()){
            m.appendReplacement(sb, m.group(1).toUpperCase());
        }
        m.appendTail(sb);
        return sb.toString();
    }

    /**
     * 下划线字符串转驼峰命名(首字母大写)
     * @param str
     * @return
     */
    public static String toUpperCamelCase(String str){
        StringBuilder result = new StringBuilder();
        // 快速检查
        if (str == null || str.isEmpty()){
            // 没必要转换
            return "";
        }else if (!str.contains(UNDER_LINE)){
            // 不含下划线，仅将首字母大写
            return str.substring(0, 1).toUpperCase() + str.substring(1);
        }
        // 用下划线将原始字符串分割
        String[] camels = str.split(UNDER_LINE);
        for (String camel : camels){
            // 跳过原始字符串中开头、结尾的下换线或双重下划线
            if (camel.isEmpty()){
                continue;
            }
            // 首字母大写
            result.append(camel.substring(0, 1).toUpperCase());
            result.append(camel.substring(1).toLowerCase());
        }
        return result.toString();
    }


    /**
     * 驼峰命名转下划线命名
     * @param str
     * @return
     */
    public static String camelToUnderline(String str) {
        if (str == null || str.trim().isEmpty()){
            return "";
        }

        int len = str.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = str.charAt(i);
            if (Character.isUpperCase(c)){
                sb.append(UNDER_LINE);
                sb.append(Character.toLowerCase(c));
            }else{
                sb.append(c);
            }
        }
        return sb.toString();
    }


    /**
     * 截取两个字符串之间的子字符串
     * @param str    原始字符串
     * @param open   开始字符串
     * @param close  结束字符串
     * @return
     */
    public static String substringBetween(String str, String open, String close){
        if(str != null && open != null && close != null){
            int start = str.indexOf(open);
            if(start != -1){
                int end = str.indexOf(close, start + open.length());
                if(end != -1){
                    return str.substring(start + open.length(), end);
                }
            }
        }
        return null;
    }

    /**
     * 判断字符串，是否包含某个字符串(不区分大小写)
     * @param str  验证字符串
     * @param strs 字符串组
     * @return 只要包含一个，就返回true
     */
    public static Boolean inStringIgnoreCase(String str, String... strs){
        if(str != null && strs != null){
            for (String s : strs){
                if(str.equalsIgnoreCase(trim(s))){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 字符串去空格
     * @param str
     * @return
     */
    public static String trim(String str){
        return str == null ? "" : str.trim();
    }
}
